package com.eqdd.yiqidian.test;

import android.content.Intent;
import android.databinding.DataBindingUtil;

import com.eqdd.multitree.tree.bean.Node;
import com.eqdd.multitree.tree.bean.TreeListViewAdapter;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.simple.SimpleTreeAdapter;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.SelectExpandActivityCustom;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.DepartmentListResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.select.SelectMultiBumenActivity;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class SelectExpandActivity extends BaseActivity {
    private RecyclerViewCustom dataBinding;
    private SlimAdapterEx<ExpandBean> slimAdapterEx;
    private ArrayList<ExpandBean> expandBeans;
    private boolean isSingle = true;
    private ExpandBean selectExpandBean;
    private int departId;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "选择部门");
        initTopRightText("提交", v -> {
            if (selectExpandBean != null) {
                Intent intent = new Intent();
                intent.putExtra("bean", selectExpandBean);
                setResult(Config.SUCCESS, intent);
                finish();
            } else {
                ToastUtil.showShort("请选择部门");
            }
        });
    }

    @Override
    public void initData() {


        departId = getIntent().getIntExtra(Config.DEPARTMENT_ID, 0);
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_56, new SlimInjector<ExpandBean>() {
            @Override
            public void onInject(ExpandBean data, IViewInjector injector) {
                injector.itemView(view -> {
                    view.setPadding(30 * data.getLevel(), 3, 3, 3);
                })
                        .text(R.id.id_treenode_label, data.getLabel())
                        .checked(R.id.checkbox, data.isCheck())
                        .visibility(R.id.id_treenode_icon, data.isLeaf() ? View.INVISIBLE : View.VISIBLE)
                        .image(R.id.id_treenode_icon, data.isExpand() ? R.mipmap.tree_ex : R.mipmap.tree_ec)
                        .clicked(R.id.view, v -> {
                            if (isSingle) {
                                data.setCheck(!data.isCheck());
                                if (selectExpandBean == data) {
                                    selectExpandBean = null;
                                } else if (selectExpandBean == null) {
                                    selectExpandBean = data;
                                } else if (selectExpandBean != null) {
                                    selectExpandBean.setCheck(false);
                                    if (slimAdapterEx.getData().indexOf(selectExpandBean) != -1) {
                                        slimAdapterEx.notifyItemChanged(slimAdapterEx.getData().indexOf(selectExpandBean));
                                    }
                                    selectExpandBean = data;
                                }
                                slimAdapterEx.notifyItemChanged(slimAdapterEx.getData().indexOf(data));

                            } else {
                                data.setCheck(!data.isCheck());
                                if (!data.isLeaf()) {
                                    ExpandBean from = data.getNextBean();
                                    int num = 1;
                                    while (from != data.getExpandBean()) {
                                        from.setCheck(data.isCheck());
                                        from = from.getNextBean();
                                        num++;
                                    }
                                    ExpandBean fromOrigin = data.getOriginNextBean();
                                    while (fromOrigin != data.getExpandBean()) {
                                        fromOrigin.setCheck(data.isCheck());
                                        fromOrigin = fromOrigin.getOriginNextBean();
                                    }
                                    slimAdapterEx.notifyItemRangeChanged(slimAdapterEx.getData().indexOf(data), num);
                                } else {
                                    slimAdapterEx.notifyItemChanged(slimAdapterEx.getData().indexOf(data));
                                }
                            }
                        });
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {

                    ExpandBean dataItem = slimAdapterEx.getDataItem(position);
                    if (!dataItem.isLeaf()) {
                        if (dataItem.isExpand()) {
                            ExpandBean nextBean = dataItem.getNextBean();
                            int num = 0;
                            while (nextBean != dataItem.getExpandBean()) {
                                slimAdapterEx.getData().remove(nextBean);
                                nextBean = nextBean.getNextBean();
                                num++;
                            }
                            dataItem.setNextBean(dataItem.getExpandBean());
                            slimAdapterEx.notifyItemRangeRemoved(position + 1, num);
                        } else {
                            ExpandBean originNextBean = dataItem.getOriginNextBean();
                            dataItem.setNextBean(dataItem.getOriginNextBean());
                            int num = 1;
                            while (originNextBean != dataItem.getExpandBean()) {
                                slimAdapterEx.getData().add(position + num, originNextBean);
                                originNextBean = originNextBean.getNextBean();
                                num++;
                            }
                            slimAdapterEx.notifyItemRangeInserted(position + 1, num - 1);
                        }
                        slimAdapterEx.notifyItemChanged(position);

                    }
                });
    }

    @Override
    public void setView() {
        expandBeans = new ArrayList<>();
        OkGo.<HttpResult<List<DepartmentListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.Select_SECTION)
                .params("CompanyId", user.getCompanyId())
                .params("ParentId", departId)
                .execute(new DialogCallBack<HttpResult<List<DepartmentListResultBean>>>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<DepartmentListResultBean>>> response) {
                        HttpResult<List<DepartmentListResultBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            List<DepartmentListResultBean> items = httpResult.getItems();
                            if (items == null || items.isEmpty()) {
                                ExpandBean expandBean = new ExpandBean();
                                expandBean.setId(AppConstent.user.getDepartId());
                                expandBean.setLabel(AppConstent.user.getDepartment());
                                Intent intent = new Intent();
                                intent.putExtra(Config.BEAN, expandBean);
                                setResult(Config.SUCCESS, intent);
                                finish();
                            }
                            initItems(null, items, 0);
                            ArrayList<ExpandBean> temp = new ArrayList<>();
                            temp.addAll(expandBeans);
                            slimAdapterEx.updateData(temp);

                        }
                    }
                });
    }

    private void initItems(DepartmentListResultBean pItem, List<DepartmentListResultBean> items, int level) {
        for (int i = 0, len = items.size(); i < len; i++) {
            DepartmentListResultBean item = items.get(i);
            expandBeans.add(item);
            item.setLevel(level);
            item.setId(Integer.parseInt(item.getDepartId()));
            item.setPid(Integer.parseInt(item.getParentId()));
            item.setLabel(item.getDepartName());
            item.setExpandBean(items.indexOf(item) == len - 1 ? (pItem == null ? pItem : pItem.getExpandBean()) : items.get(items.indexOf(item) + 1));

            if (item.getChilds() != null && item.getChilds().size() > 0) {
                item.setLeaf(false);
                item.setNextBean(item.getChilds().get(0));
                item.setOriginNextBean(item.getChilds().get(0));
                initItems(item, item.getChilds(), level + 1);
            } else {
                item.setLeaf(true);
                item.setNextBean(item.getExpandBean());
                item.setOriginNextBean(item.getExpandBean());
            }
        }

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
